package com.csap.framework.oauth2.server.service.impl;

import com.csap.framework.annotation.apidoc.Description;
import com.csap.framework.oauth2.server.entity.PlatformRolesEntity;
import com.csap.framework.oauth2.server.entity.RolesEntity;
import com.csap.framework.oauth2.server.model.PlatformRolesModel;
import com.csap.framework.oauth2.server.mybatis.PlatformRolesMapper;
import com.csap.framework.oauth2.server.service.PlatformRolesService;
import com.csap.framework.util.Assert;
import com.csap.framework.util.optional.Optional;
import com.csap.mybatisplus.extension.service.impl.BaseCsapServiceImpl;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.Set;

/**
 * <p>
 * 平台角色
 * </p>
 *
 * @author yangchengfu
 * @dateTime 2021-07-25 23:51:30
 */
@Service
@Description(value = "平台角色ServiceImpl")
public class PlatformRolesServiceImpl extends BaseCsapServiceImpl<PlatformRolesMapper, PlatformRolesEntity, PlatformRolesModel, Long> implements PlatformRolesService {

    @Override
    public Optional<Set<Long>> findDefaultRole(String platform) {
        Assert.notEmpty(platform, "平台标识不能为空");
        return lambdaQuery()
                .select(PlatformRolesEntity::getRoleId)
                .eq(PlatformRolesEntity::getPlatform, platform)
                .inner(PlatformRolesEntity::getRoleId, RolesEntity::getRoleId)
                .targetWhere()
                .eq(RolesEntity::getDefaults, Boolean.TRUE)
                .end()
                .end()
                .listField(Long.class)
                .valueOptional(i -> Sets.newHashSet(i));
    }
}
